package product;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class SendEmail extends JFrame {

	private JPanel contentPane;
	private JPanel panel;
	public JTextField username;
	private JLabel lblNewLabel;
	private JTextField subject;
	private JLabel lblNewLabel_1;
	private JPanel panel_1;
	private JScrollPane scrollPane;
	private JTextArea message;
	private JButton btnNewButton;
	Employee em;
	private JPanel panel_2;
	private JPanel panel_3;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_3;

	
	public SendEmail() {
		setTitle("To. Admin");
		setBounds(100, 100, 500, 391);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getPanel_1(), BorderLayout.SOUTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
		
		
		
		
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(SystemColor.desktop);
			panel.setLayout(new FormLayout(new ColumnSpec[] {
					ColumnSpec.decode("474px"),},
				new RowSpec[] {
					RowSpec.decode("60px"),
					RowSpec.decode("40px"),}));
			panel.add(getPanel_2(), "1, 1, fill, fill");
			panel.add(getPanel_3(), "1, 2, fill, fill");
		}
		return panel;
	}
	private JTextField getUsername() {
		if (username == null) {
			username = new JTextField();
			username.setFont(new Font("굴림", Font.PLAIN, 11));
			username.setBorder(new LineBorder(new Color(0, 0, 0)));
			username.setColumns(8);
		}
		return username;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("\uC791\uC131\uC790   ");
			lblNewLabel.setForeground(new Color(255, 255, 255));
			lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 11));
		}
		return lblNewLabel;
	}
	private JTextField getSubject() {
		if (subject == null) {
			subject = new JTextField();
			subject.setFont(new Font("굴림", Font.PLAIN, 11));
			subject.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					subject.setText("");
				}
			});
			subject.setText("제목을 입력하세요.");
			subject.setBorder(new LineBorder(new Color(0, 0, 0)));
			subject.setColumns(17);
		}
		return subject;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("\uC81C\uBAA9   ");
			lblNewLabel_1.setForeground(new Color(255, 255, 255));
			lblNewLabel_1.setFont(new Font("굴림", Font.PLAIN, 11));
		}
		return lblNewLabel_1;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setBackground(new Color(0, 0, 102));
			panel_1.setLayout(new BorderLayout(0, 0));
			panel_1.add(getBtnNewButton(), BorderLayout.EAST);
		}
		return panel_1;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBorder(null);
			scrollPane.setViewportView(getMessage());
		}
		return scrollPane;
	}
	private JTextArea getMessage() {
		if (message == null) {
			message = new JTextArea();
			message.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					message.setText("");
				}
			});
			message.setFont(new Font("Monospaced", Font.PLAIN, 11));
			message.setText("관리자에게 보낼 내용을 입력하세요.");
		}
		return message;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("보내기");
			btnNewButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					// 메일 보내야쥐
					try {
						sendMail();
					} catch (EmailException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
			btnNewButton.setFont(new Font("굴림", Font.PLAIN, 11));
		}
		return btnNewButton;
	}
	
	public void sendMail() throws EmailException{

		SimpleEmail email = new SimpleEmail(); // 선언을 한다
		email.setCharset("UTF-8");     // 인코딩을 설정한다.
		email.setHostName("smtp.gmail.com");
		email.setSmtpPort(456);
		email.setSSL(true);
		email.setTLS(true);

		email.setFrom("intotheji@gmail.com", username.getText()); //보내는 사람
		email.setAuthentication("intotheji", "dhqk8626"); //SMTP 인증 (보내는 사람 계정의 아이디와 패스워드)
		
		email.addTo("intotheji@gmail.com", "admin"); //받는사람
		email.setSubject(subject.getText()); //메일 제목
		email.setMsg(message.getText()); //메일 내용
		
		email.send(); //메일 발송
		message.setText("메일 전송이 완료되었습니다.");


	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setBorder(null);
			FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
			flowLayout.setHgap(0);
			panel_2.setBackground(new Color(0, 0, 102));
			panel_2.add(getLblNewLabel_2());
		}
		return panel_2;
	}
	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setBorder(null);
			FlowLayout flowLayout = (FlowLayout) panel_3.getLayout();
			flowLayout.setHgap(10);
			flowLayout.setVgap(10);
			panel_3.setBackground(new Color(60, 60, 124));
			panel_3.add(getLblNewLabel());
			panel_3.add(getUsername());
			panel_3.add(getLblNewLabel_3());
			panel_3.add(getLblNewLabel_1());
			panel_3.add(getSubject());
		}
		return panel_3;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			URL url = getClass().getClassLoader().getResource("sendemail.png");
			Image img = Toolkit.getDefaultToolkit().getImage(url);
			lblNewLabel_2 = new JLabel(new ImageIcon(img));
		}
		return lblNewLabel_2;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("        ");
		}
		return lblNewLabel_3;
	}
}
